﻿<%@ WebHandler Language="C#" Class="FileCS" %>

  
using System;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;

public class FileCS : IHttpHandler
{
    //    public void ProcessRequest (HttpContext context) 
    //    {
    //        int id =int.Parse( context.Request.QueryString["id"]);
    //        string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

    //        SqlConnection connection = new SqlConnection(connectionString);
    //        SqlCommand cmd = new SqlCommand("select Name, Data, ContentType from tbl_Video where Id=@Id", connection);
    //        cmd.Parameters.AddWithValue("@id",id); 
    //        try
    //        {
    //            connection.Open();
    //            SqlDataReader reader = cmd.ExecuteReader();
    //            if (reader.HasRows)
    //            {
    //                while (reader.Read())
    //                {
    //                    context.Response.ContentType = reader["Name"].ToString();
    //                    context.Response.BinaryWrite((byte[])reader["Data"]);
    //                }
    //            }
    //        }
    //        finally
    //        {
    //            connection.Close();
    //        }
    //    }

    //    public bool IsReusable 
    //    {
    //        get {
    //            return false;
    //        }
    //    }
    //}
    public void ProcessRequest(HttpContext context)
    {
        int id = int.Parse(context.Request.QueryString["id"]);
        byte[] bytes;
        string contentType;
        string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        string name;
        using (SqlConnection con = new SqlConnection(strConnString))
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "select * from tbl_DoctorVideo  where id=@Id";
                cmd.Parameters.AddWithValue("@Id", id);
                cmd.Connection = con;
                con.Open();
                SqlDataReader sdr = cmd.ExecuteReader();
                sdr.Read();
                bytes = (byte[])sdr["Video"];
                contentType = sdr["Video_Name"].ToString();
                name = sdr["Video_Name"].ToString();
                con.Close();
            }
        }
        context.Response.Clear();
        context.Response.Buffer = true;
        context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + name);
         context.Response.ContentType = contentType;
        context.Response.BinaryWrite(bytes);
        context.Response.End();

        //context.Response.ContentType = reader["Name"].ToString();
        //context.Response.BinaryWrite((byte[])reader["Video"]);
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}
